package com.baidu.camera.ui;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

import com.baidu.camera.R;
import com.baidu.camera.constants.Keys;
import com.baidu.camera.usbcamera.UVCCameraTextureView;
import com.baidu.camera.usbcamera.USBCameraHelper;

import org.androidannotations.annotations.EActivity;

@EActivity
public class USBCamera4Activity extends AppCompatActivity implements View.OnClickListener {

    private com.baidu.camera.usbcamera.UVCCameraTextureView mCameraView;
    private USBCameraHelper mHelper;
    private android.widget.ImageButton takePhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usbcamera4);
        initView();
    }

    private void initView() {
        this.mCameraView = (UVCCameraTextureView) findViewById(R.id.mCameraView);
        this.takePhoto = (ImageButton) findViewById(R.id.takePhoto);
        this.takePhoto.setOnClickListener(this);
        this.mHelper = USBCameraHelper.createUsbCameraHelper();
        this.mHelper.initCamera(this, mCameraView);
    }

    @Override
    protected void onResume() {
        this.mHelper.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        this.mHelper.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        this.mHelper.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            this.mHelper.captureImage(Keys.IMAGE_DIRECTORY);
        }
    }
}
